﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddOrder : ASPXBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRegion();
            BindDrp();
            BindGdv();
            this.txtDeliveryDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }

    void BindDrp()
    {
        GZ.Entity.EDeliveryPeriod obj = new GZ.Entity.EDeliveryPeriod();
        this.drpDeliveryPeriodId.DataSource = obj.GetDeliveryPeriod();
        this.drpDeliveryPeriodId.DataTextField = "DeliveryPeriodName";
        this.drpDeliveryPeriodId.DataValueField = "Id";
        this.drpDeliveryPeriodId.DataBind();
    }

    /// <summary>
    /// 绑定商品
    /// </summary>
    void BindGdv()
    {
        this.ProductList.DataSource = new GZ.Entity.EProductInfo().GetProducts();
        this.ProductList.DataBind();
    }

    void BindRegion()
    {
        DataTable dt = new GZ.Entity.EDeliveryArea().GetDeliveryArea();
        List<GZ.Entity.EDeliveryArea> list = new List<GZ.Entity.EDeliveryArea>();
        foreach (DataRow dr in dt.Rows)
        {
            GZ.Entity.EDeliveryArea obj = new GZ.Entity.EDeliveryArea();
            obj.Id = Convert.ToInt32(dr["Id"]);
            obj.ParentId = Convert.ToInt32(dr["ParentId"]);
            obj.DefaultCompanyName = dr["DefaultCompanyName"].ToString();
            obj.DeliveryAreaName = dr["DeliveryAreaName"].ToString();
            list.Add(obj);
        }

        var region = from a in list
                     where a.ParentId == 0
                     select a;

        this.drpRegion.DataSource = region.ToList<GZ.Entity.EDeliveryArea>();
        this.drpRegion.DataTextField = "DeliveryAreaName";
        this.drpRegion.DataValueField = "Id";
        this.drpRegion.DataBind();

        var bussiness = from a in list
                        where a.ParentId == Convert.ToInt32(this.drpRegion.SelectedValue)
                        select a;
        this.drpBusinessDistrict.DataSource = bussiness.ToList<GZ.Entity.EDeliveryArea>();
        this.drpBusinessDistrict.DataTextField = "DeliveryAreaName";
        this.drpBusinessDistrict.DataValueField = "Id";
        this.drpBusinessDistrict.DataBind();

        var office = from a in list
                     where a.ParentId == Convert.ToInt32(this.drpBusinessDistrict.SelectedValue)
                     select a;
        this.drpOffice.DataSource = office.ToList<GZ.Entity.EDeliveryArea>();
        this.drpOffice.DataTextField = "DeliveryAreaName";
        this.drpOffice.DataValueField = "Id";
        this.drpOffice.DataBind();
        this.drpOffice.Items.Insert(0, new ListItem("请选择", "-1"));

        var floor = from a in list
                    where a.ParentId == Convert.ToInt32(this.drpOffice.SelectedValue)
                    select a;
        this.drpFloor.DataSource = floor.ToList<GZ.Entity.EDeliveryArea>();
        this.drpFloor.DataTextField = "DeliveryAreaName";
        this.drpFloor.DataValueField = "Id";
        this.drpFloor.DataBind();
        this.drpFloor.Items.Insert(0, new ListItem("请选择", "-1"));

    }


    protected void drpRegion_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void drpBusinessDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void drpOffice_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new GZ.Entity.EDeliveryArea().GetDeliveryArea();
        List<GZ.Entity.EDeliveryArea> list = new List<GZ.Entity.EDeliveryArea>();
        foreach (DataRow dr in dt.Rows)
        {
            GZ.Entity.EDeliveryArea obj = new GZ.Entity.EDeliveryArea();
            obj.Id = Convert.ToInt32(dr["Id"]);
            obj.ParentId = Convert.ToInt32(dr["ParentId"]);
            obj.DefaultCompanyName = dr["DefaultCompanyName"].ToString();
            obj.DeliveryAreaName = dr["DeliveryAreaName"].ToString();
            list.Add(obj);
        }
        var floor = from a in list
                    where a.ParentId == Convert.ToInt32(this.drpOffice.SelectedValue)
                    select a;
        this.drpFloor.DataSource = floor.ToList<GZ.Entity.EDeliveryArea>();
        this.drpFloor.DataTextField = "DeliveryAreaName";
        this.drpFloor.DataValueField = "Id";
        this.drpFloor.DataBind();
        this.drpFloor.Items.Insert(0, new ListItem("请选择", "-1"));

    }
    protected void btnAddOrder_Click(object sender, EventArgs e)
    {
        Int64 OrderId = 0;
        GZ.Entity.EOrder objOrder = new GZ.Entity.EOrder();
        if (this.HCustomerId.Value.Length > 0)
        {
            objOrder.CustomerId = Convert.ToInt32(this.HCustomerId.Value);
        }
        objOrder.CustomerName = this.txtCustomerName.Text;
        objOrder.TelNum = this.txtTel.Text;
        objOrder.Email = this.txtEmail.Text;
        objOrder.RegionId = Convert.ToInt32(this.drpRegion.SelectedValue);
        objOrder.BusinessDistrictId = Convert.ToInt32(this.drpBusinessDistrict.SelectedValue);
        objOrder.OfficeId = Convert.ToInt32(this.drpOffice.SelectedValue);
        objOrder.FloorId = Convert.ToInt32(this.drpFloor.SelectedValue);
        objOrder.Address = this.txtAddress.Text;
        objOrder.Memo = this.txtMemo.Text;
        objOrder.OrderType = 2;
        objOrder.DeliveryDate = Convert.ToDateTime(this.txtDeliveryDate.Text);
        objOrder.DeliveryPeriodId = Convert.ToInt32(this.drpDeliveryPeriodId.SelectedValue);
        GZ.Entity.EOrderDetail objOrderDetail = new GZ.Entity.EOrderDetail();
        int Cnt = 0;
        decimal Price = 0;
        decimal OrderAmount = 0;
        for (int i = 0; i < this.ProductList.Rows.Count; i++)
        {
            TextBox tbCnt = this.ProductList.Rows[i].FindControl("txtCnt") as TextBox;
            Cnt = Convert.ToInt32(tbCnt.Text);
            Price = Convert.ToDecimal(this.ProductList.Rows[i].Cells[3].Text);
            OrderAmount += Cnt * Price;
        }
        objOrder.OrderAmount = OrderAmount;
        OrderId = objOrder.Insert(objOrder);

        for (int i = 0; i < this.ProductList.Rows.Count; i++)
        {
            TextBox tbCnt = this.ProductList.Rows[i].FindControl("txtCnt") as TextBox;
            Cnt = Convert.ToInt32(tbCnt.Text);
            objOrderDetail = new GZ.Entity.EOrderDetail();
            objOrderDetail.ProductId = Convert.ToInt32(this.ProductList.Rows[i].Cells[0].Text);
            objOrderDetail.Price = Convert.ToDecimal(this.ProductList.Rows[i].Cells[3].Text);
            objOrderDetail.Cnt = Cnt;
            objOrderDetail.Amount = objOrderDetail.Cnt * objOrderDetail.Price;
            objOrderDetail.OrderId = OrderId;
            if (Cnt > 0)
            {
                objOrderDetail.Insert(objOrderDetail);
            }
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "OrderOk", "<script>alert('添加成功');</script>");
        Response.Redirect("AddOrder.aspx");
    }


    protected void txtCnt_TextChanged(object sender, EventArgs e)
    {
        int Cnt = 0;
        decimal Price = 0;
        decimal OrderAmount = 0;
        for (int i = 0; i < this.ProductList.Rows.Count; i++)
        {
            TextBox tbCnt = this.ProductList.Rows[i].FindControl("txtCnt") as TextBox;
            Cnt = Convert.ToInt32(tbCnt.Text);
            Price = Convert.ToDecimal(this.ProductList.Rows[i].Cells[3].Text);
            OrderAmount += Cnt * Price;
        }
        this.lblOrderAmount.Text = "￥" + OrderAmount;
    }


    /// <summary>
    /// 查询老客户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQueryCustomer_Click(object sender, EventArgs e)
    {
        GZ.Entity.ECustomer bll = new GZ.Entity.ECustomer();
        DataTable dt = bll.GetCustomerByEmail(this.txtEmail.Text.Trim());
        if (dt.Rows.Count > 0)
        {
            this.txtCustomerName.Text = dt.Rows[0]["CustomerName"].ToString();
            this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
            this.txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            this.txtTel.Text = dt.Rows[0]["TelNum"].ToString();
            this.drpRegion.SelectedValue = dt.Rows[0]["RegionId"].ToString();
            this.drpBusinessDistrict.SelectedValue = dt.Rows[0]["BusinessDistrictId"].ToString();            this.drpOffice.SelectedValue = dt.Rows[0]["OfficeId"].ToString();
            DataTable dt_floor = new GZ.Entity.EDeliveryArea().GetDeliveryArea();
            List<GZ.Entity.EDeliveryArea> list = new List<GZ.Entity.EDeliveryArea>();
            foreach (DataRow dr in dt_floor.Rows)
            {
                GZ.Entity.EDeliveryArea obj = new GZ.Entity.EDeliveryArea();
                obj.Id = Convert.ToInt32(dr["Id"]);
                obj.ParentId = Convert.ToInt32(dr["ParentId"]);
                obj.DefaultCompanyName = dr["DefaultCompanyName"].ToString();
                obj.DeliveryAreaName = dr["DeliveryAreaName"].ToString();
                list.Add(obj);
            }
            var floor = from a in list
                        where a.ParentId == Convert.ToInt32(this.drpOffice.SelectedValue)
                        select a;
            this.drpFloor.DataSource = floor.ToList<GZ.Entity.EDeliveryArea>();
            this.drpFloor.DataTextField = "DeliveryAreaName";
            this.drpFloor.DataValueField = "Id";
            this.drpFloor.DataBind();
            this.drpFloor.Items.Insert(0, new ListItem("请选择", "-1"));
            this.drpFloor.SelectedValue = dt.Rows[0]["FloorId"].ToString();
            this.HCustomerId.Value = dt.Rows[0]["Id"].ToString();
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('老客户不存在！');</script>");
        }
    }


    /// <summary>
    /// 根据电话查找老客户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQueryCustomerByTel_Click(object sender, EventArgs e)
    {
        GZ.Entity.ECustomer bll = new GZ.Entity.ECustomer();
        GZ.Entity.EOrder bllOrder = new GZ.Entity.EOrder();
        DataTable dt = bll.GetCustomerByTel(this.txtTel.Text.Trim());
        if (dt.Rows.Count > 0)
        {
            this.txtCustomerName.Text = dt.Rows[0]["CustomerName"].ToString();
            this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
            this.txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            this.txtTel.Text = dt.Rows[0]["TelNum"].ToString();
            this.drpRegion.SelectedValue = dt.Rows[0]["RegionId"].ToString();
            this.drpBusinessDistrict.SelectedValue = dt.Rows[0]["BusinessDistrictId"].ToString();
            this.drpOffice.SelectedValue = dt.Rows[0]["OfficeId"].ToString();
            DataTable dt_floor = new GZ.Entity.EDeliveryArea().GetDeliveryArea();
            List<GZ.Entity.EDeliveryArea> list = new List<GZ.Entity.EDeliveryArea>();
            foreach (DataRow dr in dt_floor.Rows)
            {
                GZ.Entity.EDeliveryArea obj = new GZ.Entity.EDeliveryArea();
                obj.Id = Convert.ToInt32(dr["Id"]);
                obj.ParentId = Convert.ToInt32(dr["ParentId"]);
                obj.DefaultCompanyName = dr["DefaultCompanyName"].ToString();
                obj.DeliveryAreaName = dr["DeliveryAreaName"].ToString();
                list.Add(obj);
            }
            var floor = from a in list
                        where a.ParentId == Convert.ToInt32(this.drpOffice.SelectedValue)
                        select a;
            this.drpFloor.DataSource = floor.ToList<GZ.Entity.EDeliveryArea>();
            this.drpFloor.DataTextField = "DeliveryAreaName";
            this.drpFloor.DataValueField = "Id";
            this.drpFloor.DataBind();
            this.drpFloor.Items.Insert(0, new ListItem("请选择", "-1"));
            this.drpFloor.SelectedValue = dt.Rows[0]["FloorId"].ToString();
            this.HCustomerId.Value = dt.Rows[0]["Id"].ToString();
        }
        else 
        {
            dt = bllOrder.GetOrderByTelNum(this.txtTel.Text.Trim());
            if (dt.Rows.Count > 0)
            {
                this.txtCustomerName.Text = dt.Rows[0]["CustomerName"].ToString();
                this.txtAddress.Text = dt.Rows[0]["Address"].ToString();
                this.txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                this.txtTel.Text = dt.Rows[0]["TelNum"].ToString();
                this.drpRegion.SelectedValue = dt.Rows[0]["RegionId"].ToString();
                this.drpBusinessDistrict.SelectedValue = dt.Rows[0]["BusinessDistrictId"].ToString();                
                this.drpOffice.SelectedValue = dt.Rows[0]["OfficeId"].ToString();
                DataTable dt_floor = new GZ.Entity.EDeliveryArea().GetDeliveryArea();
                List<GZ.Entity.EDeliveryArea> list = new List<GZ.Entity.EDeliveryArea>();
                foreach (DataRow dr in dt_floor.Rows)
                {
                    GZ.Entity.EDeliveryArea obj = new GZ.Entity.EDeliveryArea();
                    obj.Id = Convert.ToInt32(dr["Id"]);
                    obj.ParentId = Convert.ToInt32(dr["ParentId"]);
                    obj.DefaultCompanyName = dr["DefaultCompanyName"].ToString();
                    obj.DeliveryAreaName = dr["DeliveryAreaName"].ToString();
                    list.Add(obj);
                }
                var floor = from a in list
                            where a.ParentId == Convert.ToInt32(this.drpOffice.SelectedValue)
                            select a;
                this.drpFloor.DataSource = floor.ToList<GZ.Entity.EDeliveryArea>();
                this.drpFloor.DataTextField = "DeliveryAreaName";
                this.drpFloor.DataValueField = "Id";
                this.drpFloor.DataBind();
                this.drpFloor.Items.Insert(0, new ListItem("请选择", "-1"));
                this.drpFloor.SelectedValue = dt.Rows[0]["FloorId"].ToString();
                this.HCustomerId.Value = dt.Rows[0]["CustomerId"].ToString();
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('老客户不存在！');</script>");
            }
        }
    }
}